clear; clc;
%%
getSession;
zs = sess.getZIServer;
%% Scope: Time Domain
res1 = zs.viewScopeSignal('dev3543', ziScopeInput.SigIn, [], ...            % device, ch1, ch2
                   'trig',         ziScopeTrigInput.OscPhi2, ...            % trig
                   'name',         'SCOPE: Time Domain', ...                % @ischar
                   'domain',       'time', ...                              % {'time', 'frequency'}
                   'shot_number',  1, ...                                   % @(x) x>0
                   'plot_window',  1, ...                                   % @(x) ismember(x, [1 2])
                   'rate',         ziEnumScopeSamplingRate.Rate14_6kHz, ... % @ziEnumScopeSamplingRate.test
                   'duration',     0.1, ...                                 % @(x) x>0
                   'average',      1, ...                                   % @(x) x>0
                   'slope',        ziEnumTrigEdgeSlope.Positive,...         % @ziEnumTrigEdgeSlope.test
                   'level',        0.0, ...                                 % @isnumeric
                   'isSegment',    false, ...                               % @islogical
                   'isCh1BWlimit', true, ...                                % @islogical
                   'isCh2BWlimit', true);                                   % @islogical
%% Scope FFT Average
res2 = zs.viewScopeSignal('dev3543', ziScopeInput.SigIn, ziScopeInput.AuxIn1, ... % device, ch1, ch2
                   'trig',         ziScopeTrigInput.OscPhi2, ...            % trig
                   'name',         'SCOPE: FFT', ...                        % @ischar
                   'domain',       'frequency', ...                         % {'time', 'frequency'}
                   'shot_number',  100, ...                                 % @(x) x>0
                   'plot_window',  2, ...                                   % @(x) ismember(x, [1 2])
                   'rate',         ziEnumScopeSamplingRate.Rate14_6kHz, ... % @ziEnumScopeSamplingRate.test
                   'duration',     0.1, ...                                 % @(x) x>0
                   'average',      100, ...                                 % @(x) x>0
                   'slope',        ziEnumTrigEdgeSlope.Positive,...         % @ziEnumTrigEdgeSlope.test
                   'level',        0.0, ...                                 % @isnumeric
                   'isSegment',    false, ...                               % @islogical
                   'isCh1BWlimit', true, ...                                % @islogical
                   'isCh2BWlimit', true);                                   % @islogical
%% Demod: Plotter
zs.unsubscribe_DemodSignal; 
zs.subscribe_DemodSignal('dev3543', ziDemod1.X,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3543', ziDemod2.R,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3560', ziPID3.Error, [2, 1, 2]);
res3 = zs.viewDemodSignal(5.0,...                                        % total duration
                    'name',          'Demod: Plotter', ...               % @ischar);
                    'rate',          100, ...                            % @(x) x>0);
                    'burstduration', 0.2, ...                            % @(x) x>0);
                    'mode',          ziEnumGridMode.Linear,...           % @ziEnumGridMode.test);
                    'average',       1,...                               % @(x) x>0);
                    'trig_device',   'dev3543', ...                      % @(x) ismember(x, obj.deviceSerials));
                    'trig_node',     [],    ...                          % @(x) isa(x, 'ziNode') || isempty(x));
                    'edge',          ziEnumTrigEdgeSlope.Positive,...    % @ziEnumTrigEdgeSlope.test);
                    'level',         0.0,       ...                      % @isnumeric);
                    'hysteresis',    0.0,       ...                      % @isnumeric);
                    'count',         1,         ...                      % @(x) x>0);
                    'bits',          int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'bitmask',       int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'holdofftime',   0.0,       ...                      % @(x) x>=0.0 );
                    'holdoffcount',  0,         ...                      % @(x) x>=0 );
                    'delay',         -0.1);     ...                      % @isnumeric );
%% Demod: Trigger
zs.unsubscribe_DemodSignal; 
zs.subscribe_DemodSignal('dev3543', ziDemod1.X,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3543', ziDemod2.R,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3560', ziPID3.Error, [2, 1, 2]);
res4 = zs.viewDemodSignal(5.0,...                                        % total duration
                    'name',          'Demod: Trigger', ...               % @ischar);
                    'rate',          100, ...                            % @(x) x>0);
                    'burstduration', 0.2, ...                            % @(x) x>0);
                    'mode',          ziEnumGridMode.Linear,...           % @ziEnumGridMode.test);
                    'average',       1,...                               % @(x) x>0);
                    'trig_device',   'dev3543', ...                      % @(x) ismember(x, obj.deviceSerials));
                    'trig_node',     ziDemod1.TrigIn1, ...               % @(x) isa(x, 'ziNode') || isempty(x));
                    'edge',          ziEnumTrigEdgeSlope.Positive,...    % @ziEnumTrigEdgeSlope.test);
                    'level',         0.5,       ...                      % @isnumeric);
                    'hysteresis',    0.0,       ...                      % @isnumeric);
                    'count',         5,         ...                      % @(x) x>0);
                    'bits',          int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'bitmask',       int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'holdofftime',   0.0,       ...                      % @(x) x>=0.0 );
                    'holdoffcount',  0,         ...                      % @(x) x>=0 );
                    'delay',         -0.1);     ...                      % @isnumeric );
%% Demod: Spectrum
zs.unsubscribe_DemodSignal; 
zs.subscribe_DemodSignal('dev3543', ziDemod1.X_fft_abs_avg,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3543', ziDemod2.R_fft_abs_avg,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3560', ziPID3.Error_fft_abs_avg, [2, 1, 2]);
res5 = zs.viewDemodSpectrum(512, ...                                     % frequency grid number
                    'name',          'Demod: Spectrum', ...              % @ischar);
                    'mode',          ziEnumGridMode.Exact,...            % @ziEnumGridMode.test);
                    'average',       20, ...                             % @(x) x>0);
                    'fft_window',  ziEnumFFTWindow.Hann, ...             % @ziEnumFFTWindow.test);
                    'trig_device',   'dev3543', ...                      % @(x) ismember(x, obj.deviceSerials));
                    'trig_node',     [], ...                             % @(x) isa(x, 'ziNode') || isempty(x));
                    'edge',          ziEnumTrigEdgeSlope.Positive,...    % @ziEnumTrigEdgeSlope.test);
                    'level',         0.5,       ...                      % @isnumeric);
                    'hysteresis',    0.0,       ...                      % @isnumeric);
                    'count',         5,         ...                      % @(x) x>0);
                    'bits',          int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'bitmask',       int64(1),  ...                      % @(x) isa(x, 'int64') );
                    'holdofftime',   0.0,       ...                      % @(x) x>=0.0 );
                    'holdoffcount',  0,         ...                      % @(x) x>=0 );
                    'delay',         -0.1);     ...                      % @isnumeric );
%% Sweeper
zs.unsubscribe_DemodSignal; 
zs.subscribe_DemodSignal('dev3543', ziDemod1.X_avg,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3543', ziDemod2.R_avg,   [2, 1, 1]);
zs.subscribe_DemodSignal('dev3560', ziPID3.Error_avg, [2, 1, 2]);
res6 = zs.viewParamDependance('dev3543', ziSweepNode.AuxOut1_Offset, 0.0, 3.0, ...
                        'name',             'SWEEPER', ...                  % @ischar);
                        'sweep_mode',       ziEnumSweepMode.Sequential, ... % @ziEnumSweepMode.test);
                        'npoint',           100, ...                        @(x) x>0);
                        'isLog',            false, ...                      @islogical);
                        'bandwidthcontrol', ziEnumSweepBWMode.Mannual, ...  @ziEnumSweepBWMode.test);
                        'order',            4, ...                          @(x) ismember(x, 1:8));     % fixed & auto mode
                        'bandwidth',        100, ...                        @(x) x>0);            % fixed mode
                        'maxbandwidth',     1.25e6, ...                     @(x) x>0);      % auto mode, Maximal bandwidth used in auto bandwidth mode in [Hz]. The default is 1.25MHz.
                        'bandwidthoverlap', false, ...                      @islogical); % auto mode, Sets the bandwidth overlap mode (default 0).  0 = Disable; 1 = Enable
                        'omegasuppression', 40, ...                         @(x) x>0);      % auto mode  Damping in [dB] of omega and 2omega components. Default is 40dB in favor of sweep speed. Use higher value for strong offset values or 3omega measurement methods.
                        'settling_time',    0.0, ...                        % double  Settling time before measurement is performed, in [s]
                        'settling_tc',      0, ...                          % double  Settling precision: 5 ~ low precision; 15 ~ medium precision; 50 ~ high precision
                        'settling_inaccuracy', 10e-3, ...                   % double     [1e-13, 0.1]
                        'averaging_tc',     0, ...                          % double  Min averaging time [tc]: 0 = no averaging (see also time!); 5 ~ low precision; 15 ~ medium precision; 50 ~ high precision;
                        'averaging_sample', 10, ...                         % int     Min samples to average: 1 = no averaging (if averaging/tc = 0);
                        'averaging_time',   0, ...                          % double  Min averaging time [s]
                        'phaseunwrap',      false, ...                      % bool    Enable unwrapping of slowly changing phase evolutions around the +/-180 degree boundary.
                        'sincfilter',       false, ...                      % bool    Enables the sinc filter if the sweep frequency s below 50 Hz. This will improve the sweep speed at low frequencies as omega components do not need to be suppressed by the normal low pass filter.
                        'awgcontrol',       false);                         % bool    Enable AWG control for sweeper. If enabled the sweeper will automatically start the AWG and records the sweep sample based on the even index in hwtrigger.